package org.ling.xuan.config;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.pool.DruidDataSource;
import org.ling.xuan.dao.FileDao;
import org.ling.xuan.entity.mo.CommonSetting;
import org.ling.xuan.util.FileLogUtil;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.Properties;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class TestDbConfig {
    private static volatile JdbcTemplate jdbcTemplate;
    private static final Lock lock = new ReentrantLock();
    private static volatile DataSource dataSource;

    public static JdbcTemplate getJdbcTemplate() {
        if (jdbcTemplate == null) {
            lock.lock();
            try {
                if (jdbcTemplate == null) {
                    JdbcTemplate jt = new JdbcTemplate();
                    jt.setDataSource(getDataSource());
                    jdbcTemplate = jt;
                  
                }
            } finally {
                lock.lock();
            }
        }
        return jdbcTemplate;
    }

    public static DataSource getDataSource() {
        CommonSetting commonSetting = FileDao.getCommonSetting();
        if(StrUtil.isBlank(commonSetting.getDbUrl())){
          return null;
        }
        try {
            if (dataSource == null) {
                lock.lock();
                try {
                    if (dataSource == null) {
                        String url = commonSetting.getDbUrl();
                        Properties properties = new Properties();
                        properties.setProperty("druid.stat.mergeSql", "true");
                        properties.setProperty("druid.stat.slowSqlMillis", "500");
                        DruidDataSource druidDataSource = new DruidDataSource();
                        druidDataSource.setDriverClassName(commonSetting.getDbDriver());
                        druidDataSource.setPassword(commonSetting.getDbPassword());
                        druidDataSource.setUrl(url);
                        druidDataSource.setUsername(commonSetting.getDbUser());
                        druidDataSource.setDbType(commonSetting.getDbType());
                        druidDataSource.setInitialSize(3);
                        druidDataSource.setMaxActive(10);
                        druidDataSource.setMaxWait(60000);
                        druidDataSource.setTimeBetweenEvictionRunsMillis(60000);
                        druidDataSource.setMinEvictableIdleTimeMillis(300000);
                        druidDataSource.setValidationQuery("SELECT 1 FROM DUAL");
                        druidDataSource.setTestWhileIdle(true);
                        druidDataSource.setTestOnBorrow(false);
                        druidDataSource.setTestOnReturn(false);
                        druidDataSource.setPoolPreparedStatements(true);
                        druidDataSource.setFilters("stat,wall,slf4j");
                        druidDataSource.setMaxPoolPreparedStatementPerConnectionSize(10);
                        druidDataSource.setUseGlobalDataSourceStat(true);
                        druidDataSource.setConnectProperties(properties);
                        dataSource = druidDataSource;
                    }
                } catch (SQLException e) {
                    e.printStackTrace();
                    throw new RuntimeException("数据源初始化失败");
                } finally {
                    lock.unlock();
                }
            }
            return dataSource;
        }catch (Exception e){
            FileLogUtil.
                    log("开发数据库连接失败:{}",e);
            return null;
        }
    }

    public static void refresh(){
        jdbcTemplate =null;
        getJdbcTemplate();
    }

    public static void main(String[] args) {
        try {
            getJdbcTemplate().execute("create table tbqddivdetailexpouttmp " +
                    "( " +
                    "    row_id_num       INTEGER    default 0   not null " +
                    " " +
                    ") tablespace qdiidata");
        }catch (Exception e){
            System.out.println(e.getMessage());
        }

        try {
            getJdbcTemplate().execute("drop index tbqdreqexpouttmp123123 ");
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }
}
